package com.animee.day15.slice;
import com.animee.day15.ResourceTable;
import com.animee.day15.provider.LoopPageProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/** 无限循环播放的页面*/
public class LoopPlayAbilitySlice extends AbilitySlice {

    PageSlider slider;
    String titles[] = {"休闲假日","新疆美女","迪丽热巴","夏日海滩"};
    int imgId[] = {ResourceTable.Media_lunbo1,ResourceTable.Media_lunbo2,
            ResourceTable.Media_lunbo3, ResourceTable.Media_lunbo4,};

    List<Component>list = new ArrayList<>();
    private LoopPageProvider provider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_loopplay);
        initView();

        setAutoPlay();
    }

    private void setAutoPlay() {
        Timer timer = new Timer();  //开启时间线程
        timer.schedule(new TimerTask() {
            @Override    //回到主线程
            public void run() {
                getMainTaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        //滑动到下一页
                        int currentPage = slider.getCurrentPage();
                        slider.setCurrentPage(currentPage+1);
                    }
                });
            }
        },3000,3000);
    }
    private void initView() {
        slider = findComponentById(ResourceTable.Id_loop_ps);
        for (int i = 0; i < imgId.length; i++) {
            Component component = LayoutScatter.getInstance(this)
                    .parse(ResourceTable.Layout_item_loop, null, false);
            Image iv = component.findComponentById(ResourceTable.Id_item_loop_iv);
            Text tv = component.findComponentById(ResourceTable.Id_item_loop_tv);
            tv.setText(titles[i]);
            iv.setPixelMap(imgId[i]);
            list.add(component);

        }
        provider = new LoopPageProvider(this, list);
        slider.setProvider(provider);
    }

}